package com.jstyle.jclifexd.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.AnticipateInterpolator;
import android.view.animation.BounceInterpolator;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.jstyle.jclifexd.NetWork.NetWorkUtil;
import com.jstyle.jclifexd.R;
import com.jstyle.jclifexd.model.Activitvtiesmsg;
import com.jstyle.jclifexd.model.Createddata;
import com.jstyle.jclifexd.utils.DateUtil;
import com.jstyle.jclifexd.utils.GlideCircleTransformWithBorder;
import com.jstyle.jclifexd.utils.RxBus;
import com.jstyle.jclifexd.utils.ScreenUtils;
import com.jstyle.jclifexd.utils.StatusBarUtil;
import com.jstyle.jclifexd.utils.Utils;
import java.util.HashMap;
import java.util.Map;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * 活动详情
 */
public class DetailsActivity extends BaseActivity {
    @BindView(R.id.details_img)
    ImageView details_img;
    @BindView(R.id.canjia_count)
    TextView canjia_count;//已参加人数
    @BindView(R.id.activity_name)
    TextView activity_name;//活动标题
    @BindView(R.id.details_timevalue)
    TextView details_timevalue;//起始时间
    @BindView(R.id.details_locationvalue)
    TextView details_locationvalue;//活动地点
    @BindView(R.id.activity_createdname)
    TextView activity_createdname;
    @BindView(R.id.commoit_bouttom)
    Button commoit_bouttom;
    @BindView(R.id.share_RT)
    RelativeLayout share_RT;


    @BindView(R.id.activity_detailsvalue)
    TextView activity_detailsvalue;
    Createddata createddata;
    Animation aa;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_details);
        StatusBarUtil.setRootViewFitsSystemWindows(this,true);
        StatusBarUtil.setTranslucentStatus(this);
        if (!StatusBarUtil.setStatusBarDarkTheme(this, true)) {
            StatusBarUtil.setStatusBarColor(this,0x55000000);
        }
        ButterKnife.bind(this);
        if (null != getIntent().getSerializableExtra("Createddata")) {
            createddata = (Createddata) getIntent().getSerializableExtra("Createddata");
            Glide.with(this).load(createddata.getImgUrl())
                    .apply(new RequestOptions().error(getResources().getDrawable(R.drawable.rank_top))
                            .diskCacheStrategy(DiskCacheStrategy.DATA) // 不使用磁盘缓存
                            .transform(new GlideCircleTransformWithBorder(this, 2, Color.parseColor("#ff1ad8e4")))
                    ).into(details_img);

            aa=AnimationUtils.loadAnimation(this, R.anim.translucent_scale_toself);
            aa.setInterpolator(new AnticipateInterpolator());
            details_img.startAnimation(aa);

            canjia_count.setText(createddata.getJoinNumber() + getResources().getString(R.string.participated));
            activity_name.setText(createddata.getTitle());
            String VALUE = createddata.getType().equals("act") ? getResources().getString(R.string.creator) : getResources().getString(R.string.create_team);
            activity_createdname.setText(VALUE + createddata.getCreaterName());
            details_timevalue.setText(DateUtil.gettime(this, createddata.getStart())
                    + "-" + DateUtil.gettime(this, createddata.getEnd()));
            details_locationvalue.setText(createddata.getAddress());
            activity_detailsvalue.setText(createddata.getDetail());

            if (createddata.getAvailably() == 3) {//是否已经结束
                commoit_bouttom.setEnabled(true);
                commoit_bouttom.setBackgroundColor(Color.parseColor("#C2C2C2"));
                commoit_bouttom.setText(getResources().getString(R.string.activity_ended));
            } else {
                //是否已经加入
                if (createddata.getState() == 1||NetWorkUtil.getUserId().trim().equals(createddata.getCreaterUid()+"")) {
                    commoit_bouttom.setText(getResources().getString(R.string.have_joined));
                } else {
                    if(createddata.getState()==2){//等待审核
                        commoit_bouttom.setText(getResources().getString(R.string.activity_review));
                    }else if(createddata.getState()==3){//审核未通过
                        commoit_bouttom.setText(getResources().getString(R.string.audit_failed));
                    }else{
                        switch ((int) createddata.getAvailably()) {
                            case 1://活动进行中
                                commoit_bouttom.setBackgroundColor(Color.parseColor("#1AD8E4"));
                                commoit_bouttom.setText(getResources().getString(R.string.activity_in));
                                break;
                            case 2://审核中
                                commoit_bouttom.setBackgroundColor(Color.parseColor("#C2C2C2"));
                                commoit_bouttom.setText(getResources().getString(R.string.activity_review));
                                break;
                            case 3://审核未通过
                                commoit_bouttom.setBackgroundColor(Color.parseColor("#1AD8E4"));
                                commoit_bouttom.setText(getResources().getString(R.string.audit_failed));
                                break;
                            case 4://已满员
                                commoit_bouttom.setBackgroundColor(Color.parseColor("#C2C2C2"));
                                commoit_bouttom.setText(getResources().getString(R.string.fully_occupied));
                                break;
                            case 5://结束报名
                                commoit_bouttom.setBackgroundColor(Color.parseColor("#C2C2C2"));
                                commoit_bouttom.setText(getResources().getString(R.string.has_expired));
                                break;
                        }
                    }



                }
            }

        }


    }


    @OnClick({R.id.iv_back, R.id.canjia_count, R.id.commoit_bouttom,R.id.ctivities_share})
    public void onViewClicked(View view) {
        if (Utils.isFastClick()) {
            return;
        }
        switch (view.getId()) {
            case R.id.ctivities_share:
                ScreenUtils.shareFile(this, share_RT);
                break;
            case R.id.iv_back:
                finish();
                break;
            case R.id.canjia_count:
                Intent intent = new Intent(this, DetailscountActivity.class);
                intent.putExtra("Createddata",createddata);
                startActivity(intent);
                break;
            case R.id.commoit_bouttom:
                if (createddata.getAvailably() == 3) {//是否已经结束
                showToast(getResources().getString(R.string.activity_ended));
                } else {
                    //是否已经加入
                    if (createddata.getState() != 1) {
                        if(createddata.getState()==2){//等待审核
                            showToast(getResources().getString(R.string.activity_review));
                        }else if(createddata.getState()==3){//审核未通过
                            JoinActivities(createddata);
                        }else{
                        switch ((int) createddata.getAvailably()) {
                            case 1://活动进行中
                                JoinActivities(createddata);
                                break;
                            case 2://审核中
                                showToast(getResources().getString(R.string.activity_review));
                                break;
                            case 3://审核未通过
                                JoinActivities(createddata);
                                break;
                            case 4://已满员
                                showToast(getResources().getString(R.string.fully_occupied));
                                break;
                            case 5://结束报名
                                showToast(getResources().getString(R.string.has_expired));
                                break;
                        }}

                    }
                }
                break;
        }
    }



    /**
     * 加入活动
     */
    private void JoinActivities(final Createddata aa) {
         AlertDialog alertDialog = new AlertDialog.Builder(this, R.style.appalertdialog)
                .setTitle(getString(R.string.tips))
                .setMessage(getResources().getString(R.string.joinactivityornot))
                .setCancelable(true)
                .setPositiveButton(getString(R.string.Confirm), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Map<String, Object> map = new HashMap<>();
                        map.put("actId", aa.getActId() + "");
                        map.put("uid", NetWorkUtil.getUserId());
                        NetWorkUtil.getInstance().getJstyleApi().huodongjoin(map).
                                subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<Activitvtiesmsg>() {
                            @Override
                            public void onSubscribe(Disposable d) {
                            }
                            @Override
                            public void onNext(Activitvtiesmsg activitvtiesData) {
                                RxBus.getInstance().post("reload");
                                if (7304==activitvtiesData.getMsgCode()) {
                                    showToast(getResources().getString(R.string.has_expired));
                                } else if (7302==activitvtiesData.getMsgCode()) {
                                    showToast(getResources().getString(R.string.joinsuccess));
                                    Intent intent = new Intent(DetailsActivity.this, ActivityrankingActivity.class);
                                    intent.putExtra("activitiesActivity", aa);
                                    startActivity(intent);
                                    RxBus.getInstance().post("reload");
                                    finish();
                                } else if(7303==activitvtiesData.getMsgCode()){
                                    showToast(getResources().getString(R.string.fully_occupied));
                                    commoit_bouttom.setText(getResources().getString(R.string.fully_occupied));
                                } else if(7305==activitvtiesData.getMsgCode()){
                                    showToast(getResources().getString(R.string.have_joined));
                                    commoit_bouttom.setText(getResources().getString(R.string.have_joined));
                                } else if(7307==activitvtiesData.getMsgCode()){
                                    showToast(getResources().getString(R.string.activity_review));
                                    commoit_bouttom.setText(getResources().getString(R.string.activity_review));
                                }
                            }

                            @Override
                            public void onError(Throwable e) {
                            }

                            @Override
                            public void onComplete() {
                            }
                        });
                    }
                })
                .setNegativeButton(getResources().getString(R.string.cancel), null)
                .create();
        alertDialog.show();
    }



    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(null!=aa){
            aa.cancel();
            aa=null;
        }
    }
}
